package com.hfb.mashibing.alip8.gorithm.dynamicprogramming;

public class Fib {

    public static void main(String[] args) {
        System.out.println("fib1="+fib1(10));
        System.out.println("fib2="+fib2(10));
    }
    public static int fib1(int n){
        if (n <= 0)
            return n;
        int[] memo = new int[n+1];
        memo[0] = 0;
        memo[1] = 1;
        for (int i = 2; i <= n; i++) {
            memo[i] = memo[i-1] + memo[i-2];
        }
        return memo[n];
    }
    public static int fib2(int n){
        if (n <= 0)
            return n;
        int memo_i_2 = 0;
        int memo_i_1 = 1;
        int memo_i = 1;
        for (int i = 2; i <= n; i++) {
            memo_i = memo_i_2 + memo_i_1;
            memo_i_2 = memo_i_1;
            memo_i_1 = memo_i;
        }
        return memo_i;
    }
}
